// 公共模块
import { createSlice } from "@reduxjs/toolkit";
import { useSelector } from "react-redux";
export type Tpane = {
    key: string,
    title: string,
    closable?: boolean
}
const panes: Tpane[] = [
    {
        key: "/",// 将地址作为key
        title: "主页",
        closable: false
    }
]
const commonSlice = createSlice({
    name:"common",
    initialState:{
        isLoading:false,
        panes
    },
    reducers:{
        // payload:接收更改的值
        changeIsLoading(state,{payload}){
            state.isLoading = payload;
        },
        setPanes(state,{payload}){
            if(state.panes.find(v=>v.key === payload.key)) return; 
            state.panes.push(payload);
        },
        removePanes(state,{payload}){
            state.panes.splice(payload,1);            
        },
        changeTitle(state,{payload}){
            const info:Tpane = state.panes.find(v=>v.key === payload.pathname) as Tpane;
            info.title = payload.title;
        }
    }
})
export const useCommonState=()=>useSelector((state:any)=>state.common);
export const {changeIsLoading,setPanes,removePanes,changeTitle} = commonSlice.actions;
export default commonSlice.reducer;